import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:oktoast/oktoast.dart';
import 'package:provider/provider.dart';
import 'package:sexy_tea/provider/locale_provider.dart';
import 'package:sexy_tea/provider/theme_provider.dart';
import 'package:sexy_tea/routers/not_found_page.dart';
import 'package:sexy_tea/routers/routers.dart';
import 'package:sexy_tea/utils/device_utils.dart';
import 'package:sexy_tea/views/home/home_page.dart';
import 'package:sp_util/sp_util.dart';

import 'localization/app_localizations.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  /// sp初始化
  await SpUtil.getInstance();
  runApp(MyApp());
  if (Device.isAndroid) {
    const SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class MyApp extends StatelessWidget {
  MyApp({this.home, this.theme}) {
    Routers.initRouters();
  }

  final Widget home;
  final ThemeData theme;

  @override
  Widget build(BuildContext context) {
    return OKToast(
      child: MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (_) => LocaleProvider()),
          ChangeNotifierProvider(create: (_) => ThemeProvider())
        ],
        child: Consumer2<LocaleProvider, ThemeProvider>(
          builder: (_, LocaleProvider localeProvider,
              ThemeProvider themeProvider, __) {
            return MaterialApp(
              title: '茶颜悦色',
              theme: theme ?? themeProvider.getTheme(),
              darkTheme: themeProvider.getTheme(isDarkMode: true),
              themeMode: themeProvider.getThemeMode(),
              home: home ?? HomePage(),
              onGenerateRoute: Routers.router.generator,
              localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
                AppLocalizationsDelegate(),
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate
              ],
              supportedLocales: localeProvider.supportedLocales,
              locale: localeProvider.locale,
              builder: (BuildContext context, Widget child) {
                // 初始化loading
                EasyLoading.init();
                return MediaQuery(
                  data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                  child: child,
                );
              },
              onUnknownRoute: (_) {
                return MaterialPageRoute(
                    builder: (BuildContext context) => NotFoundPage()
                );
              },
            );
          },
        ),
      ),
      backgroundColor: Colors.black54,
      textPadding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 10.0),
      radius: 20.0,
      position: ToastPosition.bottom,
    );
  }
}
